@echo off

setlocal enabledelayedexpansion

call ..\setEnv.bat

set /a is_err_if_lines=2

set info_m=
set /a ret=0


if exist run_statistic_job.log del run_statistic_job.log

set NLS_LANG=AMERICAN_AMERICA.UTF8

echo run_statistic_job.sql ... ...
sqlplus %dest_dbuser%/%dest_dbpwd%@%dest_tnsname% @run_statistic_job.sql


if exist 1.err_counts del 1.err_counts

if not exist run_statistic_job.log (
  echo run_statistic_job.log does not exist, maybe run_statistic_job does not execute, please check it.
  set info_m=with errors.
  set /a ret=-100
  goto finish
)

for %%i in (run_statistic_job.log) do (

set logfile=%%i
echo now check !logfile! ... ...

find /i "ORA-" !logfile! > 1.err_counts
set /a vars=0
echo init vars ::: !vars!
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
echo accumulate vars ::: !vars!
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "SP2-" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "PLS-" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "IMP-" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "EBER-" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "DBERROR:" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i """O/S Message:""" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)
)


:succ_1
echo check run_statistic_job.log  completed ... ...
set info_m=without errors.
set /a ret=100
goto finish

:dbscriptError_ex
echo !logfile! has errors.
set info_m=with errors.
set /a ret=-100

:finish
echo finish !info_m!

exit !ret!

endlocal


